<?php
include("mysqlcon.php");
include("userid.php");
include("ok.php");
include("error.php");

$reserv_id_list = array();
$room_id_list = array();
if(isset($_GET['ref'])){
	$room_id = $_GET['room_id'];
	$reserv_id = $_GET['reserv_id'];
	array_push($room_id_list, $room_id);
	array_push($reserv_id_list, $reserv_id);
}else{
	$reserv_id_list_temp = $_POST['checkout_reserv_id'];
	$room_id_list_temp = $_POST['checkout_room_id'];
	//echo implode(",", $room_id_list);
	$reserv_id_list = explode(",", $reserv_id_list_temp);
	$room_id_list = explode(",", $room_id_list_temp);
}

$error = false;
$list_size = sizeof($reserv_id_list);
for($i = 0;$i<$list_size;$i++){

	if($error==true) error_page($error_text);
	
	if($error==false){
		
/*       mysql_select_db($dbname,$conn); */
      mysql_query('SET NAMES utf8');
      $sql_checkindata = "SELECT checkin_id
		FROM checkin
		INNER JOIN reservation ON ( checkin.reserv_id = reservation.reserv_id ) 
		WHERE reservation.reserv_id =  '".$reserv_id_list[$i]."'";
      $raw_sql_checkindata = mysql_query($sql_checkindata);
      while ($checkindata = mysql_fetch_array($raw_sql_checkindata)){
      		//save log
	  		$save_log_checkout ="INSERT INTO checkout(reserv_id,checkin_id,userlist_id)";
	  		$save_log_checkout=$save_log_checkout."VALUES('".$reserv_id_list[$i]."','".$checkindata['checkin_id']."','$userlist_id')";
	  		if(!mysql_query($save_log_checkout,$conn)){error_page("ฐานข้อมูลผิดพลาด $strSQL");}
	  }
	  //clear room status
	  $clear_room_status = "UPDATE resort SET `room_status` = '0' WHERE `room_id` = '".$room_id_list[$i]."'";
	  if(!mysql_query($clear_room_status)){error_page("ฐานข้อมูลผิดพลาด $strSQL");} 
	  //update reserv status
	  $update_reserv_status = "UPDATE reservation SET `reserv_status` = '5' WHERE `reserv_id` = '".$reserv_id_list[$i]."'";
	  if(!mysql_query($update_reserv_status)){error_page("ฐานข้อมูลผิดพลาด $strSQL");}
	}
}

	
	
	 //save log
	  //$save_log_checkout ="INSERT INTO checkout(reserv_id,checkin_id,userlist_id)";
	  //$save_log_checkout=$save_log_checkout."VALUES('$reserv_id','$checkin_id','$userlist_id')";
	  //echo $save_log_checkout;

      /*if(!mysql_query($save_log_checkout,$conn))
				   error_page("ฐานข้อมูลผิดพลาด $strSQL");
				else 
				{*/
				  /*
 $ok_text = "<br>คุณ $customer_name $customer_surname <br>เข้าพักตั้งแต่ $reserv_begin - $reserv_end<hr><br>
				   <table id='table'>$text_room_list</table>";
*/
				   ok_page("Check Out เรียบร้อย","ระบบทำการ Check-Out ห้องพักหมายเลข : ".implode(",",$room_id_list) ." เรียบร้อยแล้ว",$ok_text,"checkout.php","");
/* 				} */
/* 				mysql_close($conn); */
?>

